
package com.oahcfly.chgame.egret.ui.actor;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.oahcfly.chgame.core.Chao;
import com.oahcfly.chgame.egret.ui.Fnt;
import com.oahcfly.chgame.egret.ui.FntParser;

/** fnt图片字体
 * 
 * @author haocao */
public class BitmapLabel extends Actor {

	private FntParser fntParser;

	private Texture texture;

	public BitmapLabel (FntParser fntParser) {
		this.fntParser = fntParser;
		texture = Chao.game.getTexture(this.fntParser.fntPngPath);
	}

	private char[] textChars = new char[0];

	public void setText (String text) {
		if (text == null) return;
		textChars = text.toCharArray();

		float width = 0;
		float height = 0;
		for (char tmpChar : textChars) {
			String txtString = String.valueOf(tmpChar);
			Fnt fnt = this.fntParser.getFntInfo(txtString);
			width += fnt.getW();
			height = fnt.getH();
		}
		setSize(width, height);
	}

	@Override
	public void draw (Batch batch, float parentAlpha) {
		// 绘制
		float x = getX();
		float y = getY();

		for (char tmpChar : textChars) {
			String txtString = String.valueOf(tmpChar);
			Fnt fnt = this.fntParser.getFntInfo(txtString);
			if (fnt == null) {
				Gdx.app.error(getClass().getSimpleName(), "can not find " + txtString + " in fnt file...");
			} else {
				batch.draw(texture, x, y, fnt.getW(), fnt.getH(), fnt.getX(), fnt.getY(), fnt.getW(), fnt.getH(), false, false);
				x += fnt.getW();
			}
		}

	}

}
